$(document).ready(function() {
  $('#document_body_textile').markItUp(mySettings);
});

function updateDecorates(action, decorate, page) {
  eval("var _l = document.getElementById('list_" + decorate.decorate_type + "');");

  if (action == "add") {
    if (_l === null || _l.undefined) {
      _l = document.createElement("ul");
      _l.id = "list_" + decorate.decorate_type;

      _c = $("#container_attached_" + decorate.decorate_type);
      _c.append(_l);
    }
    var _i = document.createElement("li");
    _i.id = "li_" + decorate.decorate_type + "_" + decorate.id;
    var _a = document.createElement("a");
    var _r = document.createElement("a");
    _a.href = "/decorates/" + decorate.id;
    _r.href = "#";
    _r.onclick = function() { requestRemove(decorate.id, page.id);return false;};
    _a.appendChild(document.createTextNode(decorate.name));
    _r.appendChild(document.createTextNode("x"));
    _i.appendChild(_a);
    _i.appendChild(document.createTextNode(" "));
    _i.appendChild(_r);
    _l.appendChild(_i);
  }
  else {
    var _t = document.getElementById("li_" + decorate.decorate_type + "_" + decorate.id);
    _l.removeChild(_t);
  }
}

function requestRemove(deco_id, doc_id) {
  $.get("/document/update_decorate?work=removeDecoFromPage&did="+ deco_id + "&docid=" + doc_id, function(data) {
    eval(data);
  });
}

function requestAdd(doc_id, deco_type) {
  eval("var deco = document.getElementById('decorate_" + deco_type + "');");
  deco_id = deco.options[deco.selectedIndex].value;

  $.get("/document/update_decorate?work=addDecoToPage&did="+ deco_id + "&docid=" + doc_id, function(data) {
    eval(data);
  });
}
